// 初始化食物GI值数据
const mongoose = require('mongoose');
const FoodGI = require('../models/FoodGI');
const config = require('../config');

// 连接数据库
mongoose.connect(config.mongoURI, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  useCreateIndex: true,
  useFindAndModify: false
}).then(() => {
  console.log('MongoDB 连接成功，开始导入数据...');
  importFoodGIData();
}).catch(err => {
  console.error('MongoDB 连接失败:', err);
  process.exit(1);
});

// 初始化数据
const foodGIData = [
  // 谷物类
  {
    name: '大米',
    giValue: 83,
    category: 'grains',
    isStapleFood: true,
    nutrition: {
      calories: 346,
      carbs: 78,
      protein: 7,
      fat: 0.6,
      fiber: 0.4
    },
    benefits: ['富含碳水化合物', '容易消化']
  },
  {
    name: '小米',
    giValue: 71,
    category: 'grains',
    isStapleFood: true,
    nutrition: {
      calories: 358,
      carbs: 76,
      protein: 9,
      fat: 2.9,
      fiber: 1.8
    },
    benefits: ['富含矿物质', '营养价值高']
  },
  {
    name: '黑米',
    giValue: 55,
    category: 'grains',
    isStapleFood: true,
    nutrition: {
      calories: 340,
      carbs: 72,
      protein: 8.5,
      fat: 2.2,
      fiber: 3.4
    },
    benefits: ['富含膳食纤维', '低GI值', '含有花青素']
  },
  {
    name: '玉米',
    giValue: 55,
    category: 'grains',
    isStapleFood: true,
    nutrition: {
      calories: 112,
      carbs: 25,
      protein: 3.4,
      fat: 1.2,
      fiber: 2.8
    },
    benefits: ['富含膳食纤维', '低GI值', '含有叶黄素']
  },
  {
    name: '玉米糁',
    giValue: 42,
    category: 'grains',
    isStapleFood: true,
    nutrition: {
      calories: 348,
      carbs: 77,
      protein: 9,
      fat: 1,
      fiber: 7
    },
    benefits: ['低GI值', '富含膳食纤维']
  },
  {
    name: '糯米',
    giValue: 83,
    category: 'grains',
    isStapleFood: true,
    nutrition: {
      calories: 350,
      carbs: 78,
      protein: 6.8,
      fat: 0.5,
      fiber: 0.9
    },
    benefits: ['易于消化']
  },
  {
    name: '荞麦',
    giValue: 54,
    category: 'grains',
    isStapleFood: true,
    nutrition: {
      calories: 343,
      carbs: 71,
      protein: 13,
      fat: 3.4,
      fiber: 10
    },
    benefits: ['低GI值', '富含蛋白质', '富含膳食纤维']
  },
  {
    name: '燕麦片',
    giValue: 66,
    category: 'grains',
    isStapleFood: true,
    nutrition: {
      calories: 379,
      carbs: 66,
      protein: 16.9,
      fat: 6.9,
      fiber: 10.6
    },
    benefits: ['富含膳食纤维', '富含蛋白质', '有助于降低胆固醇']
  },
  {
    name: '薏米',
    giValue: 78,
    category: 'grains',
    isStapleFood: true,
    nutrition: {
      calories: 358,
      carbs: 73,
      protein: 13.3,
      fat: 2.6,
      fiber: 5.9
    },
    benefits: ['富含蛋白质']
  },
  {
    name: '意大利面',
    giValue: 46,
    category: 'grains',
    isStapleFood: true,
    nutrition: {
      calories: 158,
      carbs: 31,
      protein: 5.8,
      fat: 0.9,
      fiber: 1.8
    },
    benefits: ['低GI值', '容易产生饱腹感']
  },
  
  // 豆类
  {
    name: '绿豆',
    giValue: 38,
    category: 'legumes',
    isStapleFood: false,
    nutrition: {
      calories: 347,
      carbs: 62.8,
      protein: 23.8,
      fat: 1.3,
      fiber: 16.3
    },
    benefits: ['低GI值', '富含蛋白质', '富含膳食纤维']
  },
  {
    name: '红小豆',
    giValue: 44,
    category: 'legumes',
    isStapleFood: false,
    nutrition: {
      calories: 337,
      carbs: 63,
      protein: 22,
      fat: 0.8,
      fiber: 15.2
    },
    benefits: ['低GI值', '富含蛋白质', '富含膳食纤维']
  },
  
  // 薯类
  {
    name: '红薯',
    giValue: 54,
    category: 'tubers',
    isStapleFood: true,
    nutrition: {
      calories: 86,
      carbs: 20.1,
      protein: 1.6,
      fat: 0.1,
      fiber: 3
    },
    benefits: ['低GI值', '富含维生素A', '富含膳食纤维']
  },
  {
    name: '紫薯',
    giValue: 70,
    category: 'tubers',
    isStapleFood: true,
    nutrition: {
      calories: 98,
      carbs: 22.8,
      protein: 1.4,
      fat: 0.1,
      fiber: 2.5
    },
    benefits: ['含有花青素', '富含膳食纤维']
  },
  {
    name: '土豆',
    giValue: 83,
    category: 'tubers',
    isStapleFood: true,
    nutrition: {
      calories: 79,
      carbs: 17.5,
      protein: 2.1,
      fat: 0.1,
      fiber: 2.1
    },
    benefits: ['富含维生素C', '富含钾']
  },
  {
    name: '山药',
    giValue: 52,
    category: 'tubers',
    isStapleFood: true,
    nutrition: {
      calories: 118,
      carbs: 27,
      protein: 1.5,
      fat: 0.1,
      fiber: 4
    },
    benefits: ['低GI值', '富含黏液蛋白', '有助于脾胃消化']
  },
  
  // 水果
  {
    name: '苹果',
    giValue: 36,
    category: 'fruits',
    isStapleFood: false,
    nutrition: {
      calories: 52,
      carbs: 13.8,
      protein: 0.3,
      fat: 0.2,
      fiber: 2.4
    },
    benefits: ['低GI值', '富含果胶', '富含抗氧化物']
  },
  {
    name: '香蕉',
    giValue: 52,
    category: 'fruits',
    isStapleFood: false,
    nutrition: {
      calories: 89,
      carbs: 22.8,
      protein: 1.1,
      fat: 0.3,
      fiber: 2.6
    },
    benefits: ['富含钾', '能量来源']
  },
  
  // 蔬菜
  {
    name: '西兰花',
    giValue: 20,
    category: 'vegetables',
    isStapleFood: false,
    nutrition: {
      calories: 34,
      carbs: 6.6,
      protein: 2.8,
      fat: 0.4,
      fiber: 2.6
    },
    benefits: ['低GI值', '富含维生素C', '富含抗氧化物']
  },
  {
    name: '胡萝卜',
    giValue: 47,
    category: 'vegetables',
    isStapleFood: false,
    nutrition: {
      calories: 41,
      carbs: 9.6,
      protein: 0.9,
      fat: 0.2,
      fiber: 2.8
    },
    benefits: ['富含胡萝卜素', '低GI值']
  },
  
  // 蛋白质类
  {
    name: '鸡蛋',
    giValue: 0,
    category: 'protein',
    isStapleFood: false,
    nutrition: {
      calories: 155,
      carbs: 1.1,
      protein: 12.6,
      fat: 10.6,
      fiber: 0
    },
    benefits: ['富含优质蛋白', '含多种营养素', '几乎不含碳水']
  },
  {
    name: '鸡胸肉',
    giValue: 0,
    category: 'protein',
    isStapleFood: false,
    nutrition: {
      calories: 165,
      carbs: 0,
      protein: 31,
      fat: 3.6,
      fiber: 0
    },
    benefits: ['富含优质蛋白', '低脂肪', '几乎不含碳水']
  }
];

// 导入数据
async function importFoodGIData() {
  try {
    // 清空现有数据
    await FoodGI.deleteMany({});
    console.log('已清空现有数据');
    
    // 导入新数据
    await FoodGI.insertMany(foodGIData);
    console.log(`成功导入 ${foodGIData.length} 条食物GI值数据`);
    
    // 关闭数据库连接
    mongoose.connection.close();
    console.log('数据库连接已关闭');
  } catch (error) {
    console.error('导入数据失败:', error);
    mongoose.connection.close();
  }
} 